package com.example.sms.controller;

import com.example.sms.entity.Course;
import com.example.sms.exception.R;
import com.example.sms.response.ResponseModel;
import com.example.sms.service.CourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Tag(name = "课程管理" , description = "课程管理" )
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @Operation(summary = "所有课程目录")
        @GetMapping("list")
            public Object list(){
                return R.ok(courseService.list());
            }
    @Operation(summary = "获取教师课程信息")
        @GetMapping("GetCourse")
        public Object course(Integer id){
            return R.ok(courseService.getCourses(id));
    }
    @Operation(summary = "添加课程")
    @PostMapping("AddCourse")
    public R addCourse(@RequestBody Course course){
        return R.ok(courseService.addCourse(course));
    }
    @Operation(summary = "删除课程")
    @GetMapping("DelCourse")
    public R delCourse(Integer id){
        //检查是否存在课程
        if (courseService.getById(id) == null)
            return R.fail("课程不存在");
        return R.ok(courseService.delCourse(id));
    }
    @Operation(summary = "更新课程")
    @PostMapping("UpdateCourse")
    public R<Course> updateCourse(@RequestBody Course course){
        courseService.updateById(course);
        courseService.delFromTeacherCourse(course.getId());
        courseService.addToTeacherCourse(course);
        return R.ok(courseService.getById(course.getId()));
    }
}
